package com.yeren.weixin.gzh.weixin.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yeren.weixin.gzh.weixin.config.WeixinConfig;
import com.yeren.weixin.gzh.weixin.operation.JsExecute;
import com.yeren.weixin.gzh.weixin.utils.StringUtil;

/**
 * 微信JS核心类
 * 
 * @author liubiao
 *
 */
public class WeixinJsServlet extends HttpServlet {
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		WeixinConfig wc=new WeixinConfig();
		String weixin_weixinJs_redirect_uri = wc.getWeixin_weixinJs_redirect_uri();
		StringBuffer homeUrl = req.getRequestURL();
		String queryString = req.getQueryString();
		if (StringUtil.isNotEmpty(queryString)) {
			homeUrl.append("?").append(queryString);
		}

		long timestamp = System.currentTimeMillis() / 1000;
		String nonceStr = "Wm3WZYTPz0wzccnW";
		String jsapi_ticket = JsExecute.get_jsapi_ticket("wxf9b54a0426aab166","8481d5df777deb142eb3a18b2691f95b");
		String signature = JsExecute.get_JS_Signature(jsapi_ticket,"Wm3WZYTPz0wzccnW", 1414587457,weixin_weixinJs_redirect_uri);
		String weixin_appId = wc.getWeixin_appId();
		req.setAttribute("appid", weixin_appId);
		req.setAttribute("timestamp", 1414587457);
		req.setAttribute("nonceStr", "Wm3WZYTPz0wzccnW");
		req.setAttribute("signature", signature);
		req.getRequestDispatcher("weixin_JS_demo.jsp").forward(req, resp);

	}

}
